package com.rs.game.player.questing.quests;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import org.apache.log4j.Logger;

public class QuestManager {
	
	public static void load() {
		for (File file : new File("./src/com/rs/game/player/questing/quests").listFiles()) {
			Quest<?> quest = null;
			try {
				quest = ((Quest<?>) Class.forName("com.rs.game.player.questing.quests." + file.getName().replace(".java", "")).newInstance());
			} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
				logger.warn("Could not load " + file.getName() + ": " + e.getMessage());
			}
			quests.put(quest.getName(), quest);
		}
		logger.info("Loaded " + quests.size() + " game quests.");
	}
	
	public void startQuest(String name) {
		progression.add(quests.get(name));
	}
	
	public void finishQuest(String name) {
		ListIterator<Quest<?>> it$ = progression.listIterator();
		while(it$.hasNext()) {
			Quest<?> quest = it$.next();
			if (quest.getName().equalsIgnoreCase(name)) {
				it$.remove();
			}
		}
	}
	
	public Quest<?> getQuest(String name) {
		return quests.get(name);
	}
	
	private static final Map<String, Quest<?>> quests = new HashMap<String, Quest<?>>();
	
	private static final Logger logger = Logger.getLogger(QuestManager.class.getName());
	
	private List<Quest<?>> progression = new ArrayList<Quest<?>>();
}